//
//  SelectCityViewController.m
//  DaJiang
//
//  Created by sally on 16/9/28.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CityCell.h"
#import "MainModel.h"
#import "SelectCityViewController.h"

#define ImageBeijing @"city_beijing"
#define ImageShangHai @"city_shanghai"
#define ImageHangZhou @"city_hangzhou"
#define ImageHarBin @"city_harbin"
#define ImageShenZhen @"city_shenzhen"
#define ImageGuangZhou @"city_guangzhou"
#define ImageOthers @"city_others"

#define TextBeijing @"北京"
#define TextShangHai @"上海"
#define TextHangZhou @"杭州"
#define TextHarBin @"哈尔滨"
#define TextShenZhen @"深圳"
#define TextGuangZhou @"广州"
#define TextOthers @"其他"

@interface SelectCityViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSMutableArray *locList;

@end

@implementation SelectCityViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
  [self loadData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"选择城市";
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [[NSMutableArray alloc] init];
  self.locList = [[NSMutableArray alloc] init];
  
  //从plist中获取地区列表的缓存数据
  NSMutableArray *arr = [[CacheManager shareInstance] getLocArr];
  self.locList = [[NSMutableArray alloc] initWithArray:arr];
  [self transformTableDataWithLocList:arr];
  [self.tableView reloadData];
}

#pragma mark 初始化数据
- (void)loadData {
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  [LocationModel queryLocationListWithCompletion:^(NSMutableArray *locList) {
    if (locList) {
      [[CacheManager shareInstance] cacheLocArr:locList];
      [self.tableData removeAllObjects];
      [self.locList removeAllObjects];
      self.locList = locList;
      //转换数据
      [self transformTableDataWithLocList:locList];
      [self.tableView reloadData];
    } else {
      NSError *error = [LocationModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
}

-(void)transformTableDataWithLocList:(NSMutableArray *)locList{
  for (LocationModel *loc in locList) {
    NSString *locName = loc.data.pro_user_location_name;
    if ([locName isEqualToString:TextBeijing]) {
      [self.tableData addObject:ImageBeijing];
    } else if ([locName isEqualToString:TextShangHai]) {
      [self.tableData addObject:ImageShangHai];
    } else if ([locName isEqualToString:TextHangZhou]) {
      [self.tableData addObject:ImageHangZhou];
    } else if ([locName isEqualToString:TextHarBin]) {
      [self.tableData addObject:ImageHarBin];
    } else if ([locName isEqualToString:TextShenZhen]) {
      [self.tableData addObject:ImageShenZhen];
    } else if ([locName isEqualToString:TextGuangZhou]) {
      [self.tableData addObject:ImageGuangZhou];
    } else if ([locName isEqualToString:TextOthers]) {
      [self.tableData addObject:ImageOthers];
    } else {
      [self.tableData addObject:ImageOthers];
    }
  }
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  CityCell *cell = (CityCell *) [tableView dequeueReusableCellWithIdentifier:@"CityCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"CityCell" owner:nil options:nil] lastObject];
  }
  cell.cellData = [self.tableData objectAtIndex:indexPath.row];
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  //缓存地区信息
  LocationModel *locModel = [self.locList objectAtIndex:indexPath.row];
  [[NSUserDefaults standardUserDefaults] setObject:locModel.data.pro_user_location_name forKey:UD_LAST_SELECT_LOC];
  //地址改变刷新首页数据
  [[NSNotificationCenter defaultCenter] postNotificationName:NoticeRefreshTabOneVc object:nil userInfo:nil];
  //退出
  [self.navigationController popViewControllerAnimated:YES];
}

@end
